package com.uxwine.alarm;

import java.util.Calendar;

import android.content.Context;
import android.text.format.DateFormat;

import com.uxwine.util.Log;

public class AlarmCore
{	
	
	public static long addAlarm(Context ctx, Alarm alarm)
	{
    	Log.d("new-zzm", "AlarmCore.addAlarm");
		long trigger = AlarmDao.addAlarm(ctx, alarm);
		final AlarmMgr am = AlarmMgr.getInstance(ctx);
		final Alarm snooze = am.getEnableSnooze();
		
		//如果当前正处于Snooze状态的闹钟比当前设置的闹钟要晚，则清除掉该设置，并通知闹钟已经闹过了。
		if(null != snooze && snooze.trigger > trigger)
		{
			am.clearSnooze();
			AlertMgr.getInstance(ctx).showTimeout(snooze);
		}
		
		nextAlert(ctx);
		
		return trigger;
	}
	
	public static long setAlarm(Context ctx, Alarm alarm)
	{
    	Log.d("new-zzm", "AlarmCore.setAlarm");
		long trigger = AlarmDao.setAlarm(ctx, alarm);
		clearPrefence(ctx, alarm.id, true);
		nextAlert(ctx);
		return trigger;
	}
	
	public static void enableAlarm(Context ctx, Alarm alarm, boolean enabled)
	{
    	Log.d("new-zzm", "AlarmCore.enableAlarm");
		alarm.enabled = enabled;
		AlarmDao.setAlarm(ctx, alarm);
		clearPrefence(ctx, alarm.id, true);
		nextAlert(ctx);
	}
	
	public static void enableAlarm(Context ctx, Alarm alarm, boolean enabled, boolean clear)
	{
    	Log.d("new-zzm", "AlarmCore.enableAlarm");
		alarm.enabled = enabled;
		AlarmDao.setAlarm(ctx, alarm);
		if(clear)
			clearPrefence(ctx, alarm.id, true);
		nextAlert(ctx);
	}
	
	public static void enableAlarm(Context ctx, int alarmId, boolean enabled, boolean clear)
	{
		Alarm alarm = AlarmDao.getAlarm(ctx.getContentResolver(), alarmId);
		enableAlarm(ctx, alarm, enabled, clear);
	}
	
	public static void enableAlarm(Context ctx, int alarmId, boolean enabled)
	{
		Alarm alarm = AlarmDao.getAlarm(ctx.getContentResolver(), alarmId);
		enableAlarm(ctx, alarm, enabled);
	}
	
	public static void delAlarm(Context ctx, int id)
	{
    	Log.d("new-zzm", "AlarmCore.delAlarm");
		AlarmDao.deleteAlarm(ctx, id);
		clearPrefence(ctx, id, true);
		nextAlert(ctx);
	}
	
	public static void dismissAlarm(Context ctx, int id, boolean showTimeout)
	{
    	Log.d("new-zzm", "AlarmCore.dismissAlarm");
		clearPrefence(ctx, id, !showTimeout);
		nextAlert(ctx);
	}
	
	public static boolean snoozeAlarm(Context ctx, Alarm alarm, boolean showTimeout)
	{
    	Log.d("new-zzm", "AlarmCore.snoozeAlarm");
		if(null == alarm)
			throw new NullPointerException();
		
		final AlertMgr alert = AlertMgr.getInstance(ctx);
		final AlarmMgr am = AlarmMgr.getInstance(ctx);
		
		//snooze没成功，比如超过了延迟次数，则清除snooze信息。
		if(!am.snoozeAlarm(alarm))
		{
			Log.f("snooze", "AlarmCore.snoozeAlarm - snooze failed : " + showTimeout);
			if(showTimeout)
				alert.showTimeout(alarm);
			else
				alert.cancel(alarm.id);
			am.clearSnooze();
		}
		nextAlert(ctx);
		return true;
	}
	
	public static boolean canSnoozed(Context ctx, int id)
	{
		return AlarmMgr.getInstance(ctx).canSnoozed(id);
	}
	
	/**
	 * 将Snoozed的闹钟的trigger重置。
	 * @param id
	 */
	public static void resetSnoozedAlarm(Context ctx, int id)
	{
		AlarmMgr.getInstance(ctx).resetSnoozedAlarm(id);
	}
	
	/**
	 * 清理对应id的Snooze信息。
	 * 如果id == -1,直接清理。
	 * 如果snoozed的id对应了才清理
	 * @param ctx
	 * @param id
	 */
	public static void clearPrefence(Context ctx, int id, boolean cancelAlert)
	{		
		final AlarmMgr am = AlarmMgr.getInstance(ctx);
		if(id == -1 || am.isSnoozed(id))
			am.clearSnooze();
		if(cancelAlert)
			AlertMgr.getInstance(ctx).cancel(id);
	}
	
	public static boolean nextAlert(Context ctx)
	{
		Alarm a = AlarmMgr.getInstance(ctx).nextAlarm();
		if(a == null)
		{
	    	Log.f("snooze", "AlarmCore.nextAlert: a=null");
			AlertMgr.getInstance(ctx).disable();
			return false;
		}

    	Log.f("snooze", "AlarmCore.nextAlert: " + a.label);
		if(a.type == Alarm.ALARM_SNOOZE)
		{
	    	Log.f("snooze", "AlarmCore.nextAlert: a.type == Alarm.ALARM_SNOOZE");
			AlertMgr.getInstance(ctx).snooze(a);
		}
		AlertMgr.getInstance(ctx).enable(a, a.trigger);
		return true;
	}
}
